﻿
<#
.SYNOPSIS
    Inicia o Release Management a partir do build vnext
.DESCRIPTION
    Inicia o Release Management a partir do build vnext. Script criado para ser exeuctado por uma task a partido do build
.NOTES
    File Name      : RMScript.ps1
    Author         : Igor Felix
    Prerequisite   : PowerShell V2 over Vista and upper.
    Data           : 01/02/2016
#>

param(
    [string]$stage
)

$ReleaseManagementPathExe = $env:RMPath
$urlTFSServer = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$teamProject = $env:SYSTEM_TEAMPROJECT
$BuildDefinitionName = $env:BUILD_DEFINITIONNAME
$buildNumber = $env:BUILD_BUILDNUMBER

"Executing with the following parameters:`n"
"  RM Path: $ReleaseManagementPathExe"
"  Team Foundation Server URL: $urlTFSServer"
"  Team Project: $teamProject"
"  Build Definition: $BuildDefinitionName"
"  Build Number: $BuildNumber`n"
"  Stage: $stage`n"

"Executing the following command: `n"
" `"$ReleaseManagementPathExe`" -tfs `"$urlTFSServer`" -tp `"$teamProject`" -bd `"$BuildDefinitionName`" -bn `"$BuildNumber`" -ts `"$stage`" -nologo"

$exitCode = 0

trap
{
  $e = $error[0].Exception
  $e.Message
  $e.StackTrace
  if ($exitCode -eq 0) { $exitCode = 1 }
}

try
{

    $saida = & "$ReleaseManagementPathExe" -tfs `"$urlTFSServer`" -tp `"$teamProject`" -bd `"$BuildDefinitionName`" -bn `"$BuildNumber`" -ts `"$stage`" -nologo

    if($saida -contains 'Exit: 1'){
        throw  "Error in Release Management! $saida"
    }

    Write-Host "Saida = " $saida
}
catch [System.Exception]
{
    if ($exitCode -eq 0) { $exitCode = 1 }
    Write-Host "`n$_`n" -ForegroundColor Red
}

if ($exitCode -eq 0)
{
  "`nThe script completed successfully.`n"
}
else
{
  $err = "Exiting with error: " + $exitCode + "`n"
  Write-Host $err -ForegroundColor Red
}

Pop-Location

exit $exitCode